﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace myPWMS
{
    /// <summary>
    /// F_Login.xaml 的交互逻辑
    /// </summary>
    public partial class F_Login : Window
    {
        DataBase.MyMeans MyClass = new DataBase.MyMeans();
        public F_Login()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if ((int)(Tag) == 1)
            {
                DataBase.MyMeans.Login_n = 3;
                Environment.Exit(0);
            }
            else
                if ((int)(Tag) == 2)
                Close();
        }

        private void butLogin_Click(object sender, RoutedEventArgs e)
        {

            //textName.Text = "mr";
            //textPass.Password = "mrsoft";
            textName.Text = "111";
            textPass.Password = "111";
            if (textName.Text != "" & textPass.Password != "")
            {
                SqlDataReader temDR = MyClass.getcom("select * from tb_Login where Name='" + textName.Text.Trim() + "' and Pass='" + textPass.Password.Trim() + "'");
                bool ifcom = temDR.Read();
                if (ifcom)
                {
                    DataBase.MyMeans.Login_Name = textName.Text.Trim();
                    DataBase.MyMeans.Login_ID = temDR.GetString(0);
                    DataBase.MyMeans.My_con.Close();
                    DataBase.MyMeans.My_con.Dispose();
                    DataBase.MyMeans.Login_n = (int)(Tag);
                    Close();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误！", "提示");
                    textName.Text = "";
                    textPass.Password = "";
                }
                MyClass.con_close();
            }
            else
                MessageBox.Show("请将登录信息添写完整！", "提示");
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        { 
            try
            {
                MyClass.con_open();  //连接数据库
                MyClass.con_close();
                textName.Text = "";
                textPass.Password = "";

            }
            catch
            {
                MessageBox.Show("数据库连接失败。", "提示");
                Environment.Exit(0);
            }
        }
    }
}
